<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryParams"
        size="small"
        label-width="88px"
        class="formBox"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="订单号" prop="orderNumber">
              <el-input
                v-model="queryParams.orderNumber"
                placeholder="请输入订单号"
                clearable
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入姓名"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="身份证号" prop="idCardNumber">
              <el-input
                v-model="queryParams.idCardNumber"
                placeholder="请输入身份证号"
                maxlength="18"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select
                v-model="queryParams.paymentMethod"
                placeholder="请选择支付方式"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.payment_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付类型" prop="onlineType">
              <el-select
                v-model="queryParams.onlineType"
                placeholder="请选择支付类型"
                clearable
                style="width: 100%"
              >
                <el-option label="在线支付" value="1" />
                <el-option label="离线支付" value="0" />
                <el-option label="现金充值" value="2" />
                <el-option label="在线退款" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="退款状态" prop="isRefunded">
              <el-select
                v-model="queryParams.isRefunded"
                placeholder="请选择退款状态"
                clearable
                style="width: 100%"
              >
                <el-option label="是" value="1" />
                <el-option label="否" value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="学校" prop="school">
              <el-input
                v-model="queryParams.school"
                placeholder="请输入学校"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="年级" prop="grade">
              <el-input
                v-model="queryParams.grade"
                placeholder="请输入年级"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="班级" prop="className">
              <el-input
                v-model="queryParams.className"
                placeholder="请输入班级"
                clearable
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="设备编号" prop="snCode">
          <el-input
            v-model="queryParams.snCode"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="支付时间" prop="time">
              <el-date-picker
                v-model="queryParams.time"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="search-card">
      <el-row :gutter="20">
        <el-col :span="3"> <div style="width: 100%; height: 1px"></div></el-col>
        <el-col :span="6">
          <el-card class="search-card">
            <div class="searchBox">
              <img src="@/assets/xf/icon_1.png" class="iconPublic" style="width: 20%;float: left;" alt="" />
              <div style="padding-left: 20px">
                <div style="color: #303133">全部消费金额：</div>
                <div style="margin-top: 10px">{{ detail.hz }}元</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="search-card">
            <div class="searchBox">
              <img src="@/assets/xf/icon_2.png" class="iconPublic" style="width: 20%;float: left;" alt="" />
              <div style="padding-left: 20px">
                <div style="color: #303133">支付宝小荷包消费金额：</div>
                <div style="margin-top: 10px">{{ detail.zfb }}元</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="search-card">
            <div class="searchBox">
              <img src="@/assets/xf/icon_3.png" class="iconPublic" style="width: 20%;float: left;" alt="" />
              <div style="padding-left: 20px">
                <div style="color: #303133">余额消费金额：</div>
                <div style="margin-top: 10px">{{ detail.ye }}元</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="3">
          <div></div>
        </el-col>
      </el-row>
    </el-card>
    <!-- 按钮 -->
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >
            导出
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>

    <!-- 列表 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="recordList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单号" align="center" prop="orderNumber" width="300" />
        <el-table-column label="订单金额" align="center" prop="orderAmount">
          <template slot-scope="scope">
            {{ scope.row.orderAmount ? scope.row.orderAmount : "0" }}元
          </template>
        </el-table-column>
        <el-table-column label="余额" align="center" prop="walletBalance">
          <template slot-scope="scope">
            {{ scope.row.walletBalance ? scope.row.walletBalance : "0" }}元
          </template>
        </el-table-column>

        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column
          label="身份证号"
          align="center"
          prop="idCardNumber"
          width="180"
        />
        <el-table-column label="设备编号" align="center" prop="snCode" width="180" />
        <el-table-column label="设备名称" align="center" prop="deviceName" width="150" />
        <el-table-column label="商家名称" align="center" prop="merName" />
        <!-- <el-table-column label="订单来源" align="center" prop="orderSource">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.payment_method"
              :value="scope.row.orderSource"
            />
          </template>
        </el-table-column> -->
        <el-table-column label="学校" align="center" prop="school" width="200" />
        <el-table-column label="年级" align="center" prop="grade" />
        <el-table-column label="班级" align="center" prop="className" />
        <el-table-column label="身份" align="center" prop="identity" />
        <el-table-column label="支付方式" align="center" prop="paymentMethod" width="140">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.payment_method"
              :value="scope.row.paymentMethod"
            />
          </template>
        </el-table-column>
        <el-table-column label="支付类型" align="center" prop="onlineType">
          <template slot-scope="scope">
            <span v-if="scope.row.onlineType == '1'">在线支付</span>
            <span v-if="scope.row.onlineType == '0'">离线支付</span>
            <span v-if="scope.row.onlineType == '2'">现金充值</span>
            <span v-if="scope.row.onlineType == '3'">在线退款</span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="已退款" align="center" prop="isRefunded">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.is_refunded" :value="scope.row.isRefunded" />
          </template>
        </el-table-column>
        <el-table-column label="退款金额" align="center" prop="refundAmount" />
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
          v-if="userId != '109' && userId != '108'"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="padding_4_10"
              @click="refundStatusFun(scope.row)"
              v-if="scope.row.statusName == '支付成功' && scope.row.orderFinish != '1'"
              >因错退款
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改消费记录对话框 -->
    <el-dialog title="应错退款" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="充值订单号" prop="rechargeOrderNo">
          <el-input v-model="form.orderNumber" placeholder="请输入充值订单号" readonly />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" readonly />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证号" readonly />
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校" readonly />
        </el-form-item>
        <el-form-item label="充值金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" placeholder="请输入充值金额" readonly />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount" class="is-required">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款理由" prop="refundReason" class="is-required">
          <el-input v-model="form.refundReason" placeholder="请输入退款理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  consumptionRefund,
  summary
} from "@/api/order/consumption";
import { time } from "echarts";
export default {
  name: "consumption",
  dicts: ["payment_method", "payment_method", "is_refunded"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消费记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idCardNumber: null,
        personId: null,
        school: null,
        grade: null,
        className: null,
        identity: null,
        deviceNumber: null,
        orderNumber: null,
        orderSource: null,
        orderAmount: null,
        paymentMethod: null,
        paymentTime: null,
        isRefunded: null,
        refundAmount: null,
        time: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        idCardNumber: [{ required: true, message: "身份证号不能为空", trigger: "blur" }],
        personId: [{ required: true, message: "人员ID不能为空", trigger: "blur" }],
        orderNumber: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
        orderAmount: [{ required: true, message: "订单金额不能为空", trigger: "blur" }],
        paymentTime: [{ required: true, message: "支付时间不能为空", trigger: "blur" }],
      },
      userId: "", detail: {
        xhb: 0.0,
      },
    };
  },
  watch: {
    "queryParams.time": function (newVal) {
      console.log(newVal);
      if (!newVal) {
        this.queryParams.time=[];
        this.queryParams.paymentTimeStart = null;
        this.queryParams.paymentTimeEnd = null;
      }
    },
  },
  created() {
    this.userId = window.sessionStorage.getItem("userId");
    this.getList();
    this.summaryFun();
  },
  activated() {
    this.$forceUpdate();
  },
  methods: {
    /** 查询消费记录列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.time.length == 2) {
        this.queryParams.paymentTimeStart = this.queryParams.time[0];
        this.queryParams.paymentTimeEnd = this.queryParams.time[1];
      }
      listRecord(this.queryParams).then((response) => {
        var list = response.rows;
        list.forEach((data) => {
          if (data.isRefunded == "1") {
            if (data.refundAmount != data.orderAmount) {
              data.isRefunded = 2;
            }
          }
        });
        this.recordList = list;
        this.total = response.total;
        this.loading = false;
      });
    },

    summaryFun() {
      var obj = {...this.queryParams};
      if (obj.time.length == 2) {
        obj.paymentTimeStart = obj.time[0];
        obj.paymentTimeEnd = obj.time[1];
      }
      obj.pageNum=null;
      obj.pageSize=null;
      summary(obj).then((res) => {
        if (res.code == 200) {
          this.detail = res.data;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        idCardNumber: null,
        personId: null,
        school: null,
        grade: null,
        className: null,
        identity: null,
        deviceNumber: null,
        orderNumber: null,
        orderSource: null,
        orderAmount: null,
        paymentMethod: null,
        paymentTime: null,
        isRefunded: null,
        refundAmount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.summaryFun();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");

      this.queryParams.paymentTimeStart = null;
      this.queryParams.paymentTimeEnd = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      安踏;
    },

    refundStatusFun(options) {
      this.form = options;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.form.refundAmount) {
        return this.$modal.msgError("请输入正确的退款金额");
      }
      if (!this.form.refundReason) {
        return this.$modal.msgError("请输入退款原因");
      }
      this.$refs["form"].validate((valid) => {
        if (this.form.refundAmount == 0) {
          return this.$modal.msgError("请输入正确的退款金额");
        }
        if (this.form.id != null) {
          consumptionRefund(this.form).then((response) => {
            this.$modal.msgSuccess("退款成功");
            this.open = false;
            this.getList();
          });
        } else {
          consumptionRefund(this.form).then((response) => {
            this.$modal.msgSuccess("退款成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/order/consumption/export",
        {
          ...this.queryParams,
        },
        "消费记录" + `_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
